package com.atgugui.educms.controller;


import com.atgugui.commonutils.Result;
import com.atgugui.educms.entity.CrmBanner;
import com.atgugui.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * 管理员操作对banner的CURD操作
 * </p>
 * @author testjava
 * @since 2021-09-09
 */
@Api(tags = "Banner后台管理控制层接口")
@RestController
@RequestMapping("/educms/bannerAdmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    @ApiOperation(value = "分页查询Banner")
    @GetMapping("/pageBanner/{current}/{limit}")
    public Result pageBanner(@PathVariable Long current, @PathVariable Long limit) {
        Page<CrmBanner> page = new Page<>(current,limit);
        crmBannerService.page(page,null);
        long total = page.getTotal();
        List<CrmBanner> records = page.getRecords();
        return Result.ok().data("total",total).data("items",records);
    }

    @ApiOperation(value = "添加banner")
    @PostMapping("/addBanner")
    public Result addBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.save(crmBanner);
        return Result.ok();
    }

    @ApiOperation(value = "删除banner")
    @DeleteMapping("/deleteBanner/{bannerID}")
    public Result deleteBanner(@PathVariable String bannerID) {
        crmBannerService.removeById(bannerID);
        return Result.ok();
    }

    @ApiOperation(value = "更新banner")
    @PostMapping("/updateBanner")
    public Result updateBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.updateById(crmBanner);
        return Result.ok();
    }

    @ApiOperation(value = "查询banner")
    @GetMapping("/findBanner/{bannerId}")
    public Result findBannerById(@PathVariable String bannerId) {
        CrmBanner crmBanner = crmBannerService.getById(bannerId);
        return Result.ok().data("banner",crmBanner);
    }
}

